<!--
 * @Date: 2019-11-06 10:43:32
 * @LastEditors: peng qiao
 * @LastEditTime: 2020-02-22 12:48:11
 -->
<template>
  <div class="searchList">
    <van-list>
      <van-cell v-if="notFromSearch" class="return-style" @click="returnCity">
        <van-icon name="arrow-left" color="#DC4630" /><span
          class="return-text-style"
          >返回上一级</span
        >
      </van-cell>
      <van-cell v-else class="return-style" @click="returnCityList">
        <van-icon name="arrow-left" color="#DC4630" /><span
          class="return-text-style"
          >返回城市列表</span
        >
      </van-cell>
      <van-cell
        v-for="item in list"
        :key="item.id"
        :title="item.name"
        @click="searchListSel(item)"
      />
    </van-list>
  </div>
</template>

<script>
import api from "@/controller/Interface.js";
import constant from "@/controller/constant.js";
export default {
  props: { villageList: Array, canReturn: Boolean },
  name: "SearchList",
  components: {},
  data() {
    return {
      list: [],
      notFromSearch: true
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.list = this.villageList;
    this.notFromSearch = this.canReturn
  },
  watch: {
    villageList: {
      handler: function(val) {
        this.list = val;
      },
      deep: true
    },
    canReturn: {
      handler: function(val) {
        this.notFromSearch = val;
      },
      deep: true
    },
  },
  methods: {
    searchListSel(data) {
      api.user_property_v1_select_neighborhood(data.id).then(res => {
        if (res.code === "0") {
          if (localStorage.getItem('historyList')) {
            JSON.parse(localStorage.getItem('historyList')).push(data)
          } else {
            localStorage.setItem('historyList',JSON.stringify([data]))
          }
          this.$emit("searchListSel", data);
        } else {
          if (res && res.msg) {
            this.$toast(res.msg);
          } else {
            this.$toast(constant.unusualMsg);
          }
        }
      })
    },
    returnCity(data) {
      this.$emit("returnCommunity", data);
    },
    returnCityList(data) {
      this.$emit("returnCityList", data);
    },
  }
};
</script>

<style lang="scss" scoped>
.searchList {
  width: 100%;
  height: 100%;
  background: #ffffff;
  .return-style {
    display: flex;
    align-items: center;
    .return-text-style {
      margin-left: 10px;
    }
  }
}
</style>
